@echo off

REM initial creation: 2008-12-28 16:28:36
REM first finish: 2008-12-30 10:30:28

pushd %CD%
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

REM No quotes even the path contains spaces!
set pic_loc=d:\zerox\picture\movie
REM Add quotes appropriately
REM set clipboard=d:\zerox\bin\clipboard.exe
set clipboard=D:\zerox\bin\nircmdc.exe
if not exist %clipboard% echo ERROR: %clipboard%: not exists&&goto error

REM Command line argument
if "%~1"=="" goto usage

REM Goto working directory
set dest_path="%~1"
if not exist "!dest_path!" echo ERROR: !dest_path!: not exists&&goto error
cd /d "!dest_path!"
if errorlevel 1 (
    echo ERROR: cannot chdir: !dest_path!&&goto error
) else (
    echo Successfully changed directory to:
    echo  !CD!
)

REM Check picture file(s)
REM No quotes! Better not use spaces, pic_no only empty or digits
set pic_no=
set pic_pre=poster
REM If the picture location (pic_loc) does not exist, goto intro.txt creation
REM directly
if not exist "!pic_loc!" echo WARNING: !pic_loc!: not exists&&goto intro_check

for %%i in (!pic_loc!\*.jpg !pic_loc!\*.bmp !pic_loc!\*.gif !pic_loc!\*.png) do (
    set pic_file=%%~fi
    set pic_ext=%%~xi
    REM echo Processing file "!pic_file!"...
    call :pic_check
    echo MOVE "!pic_file!" "!pic_pre!!pic_no!!pic_ext!"
    MOVE "!pic_file!" "!pic_pre!!pic_no!!pic_ext!"
    set /a pic_no=pic_no+1
    if !pic_no!==1 set pic_no=2
)

REM check intro.txt file(s)
REM No quotes! Better not use spaces, intro_no only empty or digits
set intro_no=
set intro_pre=intro

:intro_check
if exist "!intro_pre!!intro_no!.txt" (
    set /a intro_no=intro_no+1
    if !intro_no!==1 set intro_no=2
    goto intro_check
)
echo Creating "!intro_pre!!intro_no!.txt"...
%clipboard% clipboard addfile "!intro_pre!!intro_no!.txt"

REM over
goto end

REM Subroutine "pic_check"
:pic_check
:pic_check_loop
    if exist "!pic_pre!!pic_no!.*" (
        set /a pic_no=pic_no+1
        if !pic_no!==1 set pic_no=2
        goto pic_check_loop
    )
    goto :eof

REM ======================================================================

:usage
echo Usage: %~nx0 PATH
goto end

:error
pause
:end
endlocal
popd
